package thread;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

public class Demo29 {
    public static void main(String[] args) {
        // 应用：生产消费模型
        BlockingQueue<Integer> queue = new ArrayBlockingQueue<>(1000);
        // 生产者
        Thread t1 = new Thread(() -> {
            int count = 0;
            try {
                while (true) {
                    queue.put(count);
                    System.out.println("生产了：" + count);
                    count++;
                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });

        // 消费者
        Thread t2 = new Thread(() -> {
            try {
                while (true) {
                    Integer elem = queue.take();
                    System.out.println("消费了：" + elem);
//                    Thread.sleep(1000);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        t1.start();
        t2.start();
    }
}
